﻿var core = (function () {
    var app;
    function initIndex() {
        try {
            initPage();
            //initApp();
            initTask();

        } catch (e) {
            com._cLog("initIndex", e.description, "error");
        }
    }

    function initPage() {
        try {
            com._cResizeWindow();
            com._cFullScreen();
            com._cInitPage();
            var _d = com._cFormatDateTime();
            $("#mcdate").html("日期：" + _d.year + "年" + _d.month + "月" + _d.date + "日" + " " + _d.day);
            var _t = soul.configs["taskTitle"];
            $("body").layout("panel", "west").panel({ title: _t });
            addTab("个人主页", com._cFormatPages("Home/home.html", "home"), "user_home", "mtabmenu", false);
            $(".mshutdown").click(function () {
                $.messager.confirm("退出系统", "是否确定退出?", function (_b) {
                    if (_b) {
                        window.location.href = "login.html";
                    }
                });
            });
        } catch (e) {
            com._cLog("initPage", e.description, "error");
        }
    }

    function initTask() {
        try {
            //var _atl = service.EasyGetData("apptasklist", false);
            var _$atl = com._cGetXmlObj("data/Menu.xml");
            var _app = [];
            //            _$atl.find("AppTaskList").each(function (i, n) {
            //                var _gid = $(n).find("Guid").text();
            //                var _tp = $(n).find("TaskType").text();
            //                if ($.inArray(_tp, _app) == "-1") {
            //                    _app.push(_tp);
            //                    $("#mnav").accordion("add", { title: _tp });
            //                }
            //            });
            _$atl.find("FirstMenu").each(function (i, n) {
                //                var _gid = $(n).find("Guid").text();
                var _tp = $(n).attr("MasterType");
                if ($.inArray(_tp, _app) == "-1") {
                    _app.push(_tp);
                    $("#mnavg").append('<li><span><img src="css/skytheme/images/n1.gif" /></span>' + $(n).attr("MasterType") + '</li>');
                }
            });

            $("#mnavg li").click(function () {
                $($("#mnav").accordion("panels")).each(function (s, m) {
                    $("#mnav").accordion("remove", m.panel("options").title);
                });
                var mm = $(this).text();
                var task = [];
                _$atl.find("FirstMenu[MasterType='" + mm + "']").each(function (i, n) {
                    if (!$("#mnav").accordion("getPanel", mm)) {
                        task.push($(n).find("SecondMenu").attr("SubType"));
                        $("#mnav").accordion("add", { title: $(n).find("SecondMenu").attr("SubType") });
                    }
                });

                _$atl.find("SecondMenu").each(function (i, n) {
                    if ($.inArray($(n).attr("SubType"), task) == "-1") {
                        return;
                    }
                    var _ct = "<ul>";
                    _$atl.find("TaskInfo").each(function (j, k) {
                        if ($(k).parent().attr("SubType") == $(n).attr("SubType")) {
                            var _icon = $(k).attr("IconUrl") == "" ? "user_default" : $(k).attr("IconUrl");
                            _ct += "<li style='padding-left:30px;background-position-x:6%;background-position-y:50%;cursor:pointer;' path='" + $(k).attr("FormUrl") + "' guid='" + $(k).attr("Guid") + "' icon='" + _icon + "' class='" + _icon + "'>" + $(k).attr("Name") + "</li>";
                        }
                    });
                    _ct += "</ul>";

                    $("#mnav").accordion("getPanel", $(n).attr("SubType")).panel({ content: _ct });
                });

                $("#mnav").accordion({ animate: true });

                $('#mnav li').click(function () {
                    if ($(this).hasClass("selected")) {
                        return false;
                    }
                    $('#mnav li').removeClass("selected");
                    $(this).addClass("selected");
                    var _guid = $(this).attr("guid");
                    //                var _stfi = service.EasyGetData("searchtaskforminfo", false, _guid);
                    //                var _$stfi = com._cGetXmlObj(_stfi);
                    var _spath = com._cFormatPages($(this).attr("path"), _guid);
                    addTab($(this).text(), _spath, $(this).attr("icon"), "mtabmenu", true);
                }).hover(function () {
                    if ($(this).hasClass("selected")) {
                        return false;
                    }
                    $(this).addClass("hover");
                }, function () {
                    $(this).removeClass("hover");
                });

                tabClose("mtabmenu");
                tabCloseEven();

                $("#mtabs").tabs({
                    tools: [{
                        text: "功能项目",
                        handler: function () {
                            com._cFullScreen();
                        }
                    }]
                });

                $('#mmenus').menubutton({
                    menu: '#mmenu'
                });
            });
            //$("#mnav").accordion("add", { title: "Demo功能说明" });

            //            $(_app).each(function (i, n) {
            //                var _ct = "<ul>";
            //                _$atl.find("TaskType").each(function (j, k) {
            //                    if ($(k).text() == n) {
            //                        var _icon = $(k).parent().find("Icon").text() == "" ? "user_default" : $(k).parent().find("Icon").text();
            //                        _ct += "<li style='padding-left:30px;background-position-x:6%;background-position-y:50%;cursor:pointer;' guid='" + $(k).parent().find("Guid").text() + "' icon='" + _icon + "' class='" + _icon + "'>" + $(k).parent().find("Name").text() + "</li>";
            //                    }
            //                });
            //                _ct += "</ul>";

            //                $("#mnav").accordion("getPanel", n).panel({ content: _ct });
            //            });

            //$("#mnav").accordion("getPanel", "Demo功能说明").panel({ content: "<ul><li>版本功能说明</li><li>内部参数说明</li></ul>" });

            
        } catch (e) {
            com._cLog("initTask", e.description, "error");
        }
    }

    //    function initApp() {
    //        try {
    //            var _al = service.EasyGetData("applist");
    //            var _$al = com._cGetXmlObj(_al);
    //            var msystem = $("#msystem");
    //            _$al.find("ListOUDataInfo").each(function (i, n) {
    //                var _mt = $(n).attr("MasterType");
    //                msystem.after("<div><span>" + _mt + "</span><div style='width:150px;' name=\"" + _mt + "\"></div></div>");
    //                $(n).find("OUDataInfo").each(function (j, k) {
    //                    $("div[name='" + _mt + "']").append("<div id=\"" + $(k).attr("GUID") + "\">" + $(k).attr("NAME") + "</div>");
    //                });
    //            });

    //        } catch (e) {
    //            com._cLog("initApp" + e.description, "error");
    //        }
    //    }

    function addTab(_t, _u, _i, _m, _c) {
        try {
            if (!$('#mtabs').tabs('exists', _t)) {
                $('#mtabs').tabs('add', {
                    title: _t,
                    content: com._cCreateFrame(_u),
                    closable: _c,
                    icon: _i
                });
            } else {
                $('#mtabs').tabs('select', _t);
                $('#mmenu-tabupdate').click();
            }
            tabClose(_m);
        } catch (e) {
            com._cLog("addTab", e.description, "error");
        }
    }

    function tabClose(_m) {
        try {
            $(".tabs-inner").dblclick(function () {
                var subtitle = $(this).children(".tabs-closable").text();
                $('#mtabs').tabs('close', subtitle);
            });
            $(".tabs-inner").bind('contextmenu', function (e) {
                $('#' + _m).menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });

                var subtitle = $(this).children(".tabs-closable").text();

                $('#' + _m).data("currtab", subtitle);
                $('#mtabs').tabs('select', subtitle);
                return false;
            });
        } catch (e) {
            com._cLog("tabClose" + e.description, "error");
        }
    }

    function tabCloseEven() {
        var _thome = "个人主页";
        //刷新
        $('#mmenu-tabupdate').click(function () {
            var currTab = $('#mtabs').tabs('getSelected');
            var url = $(currTab.panel('options').content).attr('src');
            $('#mtabs').tabs('update', {
                tab: currTab,
                options: {
                    content: com._cCreateFrame(url)
                }
            })
        })
        //关闭当前
        $('#mmenu-tabclose').click(function () {
            var currtab_title = $('#mtabmenu').data("currtab");
            if (_thome == currtab_title) {
                return;
            }
            $('#mtabs').tabs('close', currtab_title);
        })
        //全部关闭
        $('#mmenu-tabcloseall').click(function () {
            $('.tabs-inner span').each(function (i, n) {
                var t = $(n).text();
                if (_thome == t) {
                    return;
                }
                $('#mtabs').tabs('close', t);
            });
        });
        //关闭除当前之外的TAB
        $('#mmenu-tabcloseother').click(function () {
            $('#mmenu-tabcloseright').click();
            $('#mmenu-tabcloseleft').click();
        });
        //关闭当前右侧的TAB
        $('#mmenu-tabcloseright').click(function () {
            var nextall = $('.tabs-selected').nextAll();
            if (nextall.length == 0) {
                return false;
            }
            nextall.each(function (i, n) {
                var t = $('a:eq(0) span', $(n)).text();
                if (_thome == t) {
                    return;
                }
                $('#mtabs').tabs('close', t);
            });
            return false;
        });
        //关闭当前左侧的TAB
        $('#mmenu-tabcloseleft').click(function () {
            var prevall = $('.tabs-selected').prevAll();
            if (prevall.length == 0) {
                return false;
            }
            prevall.each(function (i, n) {
                var t = $('a:eq(0) span', $(n)).text();
                if (_thome == t) {
                    return;
                }
                $('#mtabs').tabs('close', t);
            });
            return false;
        });

        //退出
        $("#mmenu-exit").click(function () {
            $('#mtabmenu').menu('hide');
        })
    }

    return {
        Init: function () {
            initIndex();
        },
        AddTab: function (_t, _u, _i, _m, _c) {
            addTab(_t, _u, _i, _m, _c);
        },
        Messager: $.messager
    }
})()